﻿namespace Chain.DAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class Proposal
    {
        public int Add(Chain.Model.Proposal model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into Proposal(");
            builder.Append("ProposalContent,MemID,MemMobile,ProposalTime)");
            builder.Append(" values (");
            builder.Append("@ProposalContent,@MemID,@MemMobile,@ProposalTime)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ProposalContent", SqlDbType.NVarChar, 0xfa0), new SqlParameter("@MemID", SqlDbType.Int, 4), new SqlParameter("@MemMobile", SqlDbType.NVarChar, 50), new SqlParameter("@ProposalTime", SqlDbType.DateTime) };
            cmdParms[0].Value = model.ProposalContent;
            cmdParms[1].Value = model.MemID;
            cmdParms[2].Value = model.MemMobile;
            cmdParms[3].Value = model.ProposalTime;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int ProposalID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from Proposal ");
            builder.Append(" where ProposalID=@ProposalID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ProposalID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ProposalID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string ProposalIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from Proposal ");
            builder.Append(" where ProposalID in (" + ProposalIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int ProposalID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from Proposal");
            builder.Append(" where ProposalID=@ProposalID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ProposalID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ProposalID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ProposalID,ProposalContent,MemID,MemMobile,ProposalTime ");
            builder.Append(" FROM Proposal ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" ProposalID,ProposalContent,MemID,MemMobile,ProposalTime ");
            builder.Append(" FROM Proposal ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.ProposalID desc");
            }
            builder.Append(")AS Row, T.*  from Proposal T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public Chain.Model.Proposal GetModel(int ProposalID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 ProposalID,ProposalContent,MemID,MemMobile,ProposalTime from Proposal ");
            builder.Append(" where ProposalID=@ProposalID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ProposalID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ProposalID;
            Chain.Model.Proposal proposal = new Chain.Model.Proposal();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["ProposalID"] != null) && (set.Tables[0].Rows[0]["ProposalID"].ToString() != ""))
            {
                proposal.ProposalID = int.Parse(set.Tables[0].Rows[0]["ProposalID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["ProposalContent"] != null) && (set.Tables[0].Rows[0]["ProposalContent"].ToString() != ""))
            {
                proposal.ProposalContent = set.Tables[0].Rows[0]["ProposalContent"].ToString();
            }
            if ((set.Tables[0].Rows[0]["MemID"] != null) && (set.Tables[0].Rows[0]["MemID"].ToString() != ""))
            {
                proposal.MemID = new int?(int.Parse(set.Tables[0].Rows[0]["MemID"].ToString()));
            }
            if ((set.Tables[0].Rows[0]["MemMobile"] != null) && (set.Tables[0].Rows[0]["MemMobile"].ToString() != ""))
            {
                proposal.MemMobile = set.Tables[0].Rows[0]["MemMobile"].ToString();
            }
            if ((set.Tables[0].Rows[0]["ProposalTime"] != null) && (set.Tables[0].Rows[0]["ProposalTime"].ToString() != ""))
            {
                proposal.ProposalTime = new DateTime?(DateTime.Parse(set.Tables[0].Rows[0]["ProposalTime"].ToString()));
            }
            return proposal;
        }

        public DataSet GetProposalInfo(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = "Proposal,Mem,SysShop";
            string[] column = new string[] { "ProposalID,MemName,MemCard,MemSex,ProposalContent,Proposal.MemMobile,ShopName,ProposalTime" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "ProposalID", false, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM Proposal ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Update(Chain.Model.Proposal model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update Proposal set ");
            builder.Append("ProposalContent=@ProposalContent,");
            builder.Append("MemID=@MemID,");
            builder.Append("MemMobile=@MemMobile,");
            builder.Append("ProposalTime=@ProposalTime");
            builder.Append(" where ProposalID=@ProposalID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ProposalContent", SqlDbType.NVarChar, 0xfa0), new SqlParameter("@MemID", SqlDbType.Int, 4), new SqlParameter("@MemMobile", SqlDbType.NVarChar, 50), new SqlParameter("@ProposalTime", SqlDbType.DateTime), new SqlParameter("@ProposalID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.ProposalContent;
            cmdParms[1].Value = model.MemID;
            cmdParms[2].Value = model.MemMobile;
            cmdParms[3].Value = model.ProposalTime;
            cmdParms[4].Value = model.ProposalID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

